package com.jiangtai.training.ui.main

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.LoRaManager
import android.support.v4.app.Fragment
import android.util.Log
import com.jiangtai.training.R
import com.jiangtai.training.base.BaseActivity
import com.jiangtai.training.constant.Constant
import com.jiangtai.training.ui.practice.PracticeFragment
import com.jiangtai.training.ui.setting.SettingFragment
import com.jiangtai.training.ui.task.TaskFragment
import com.jiangtai.training.util.Preference
import kotlinx.android.synthetic.main.activity_main.*
import org.litepal.LitePal

/**
 * Created by heCunCun on 2021/3/9
 */
class MainActivity : BaseActivity() {
    private var practiceFragment: PracticeFragment? = null
    private var taskFragment: TaskFragment? = null
    private var settingFragment:SettingFragment?=null
    private var currentFragment: Fragment? = null
    private var index = 1
    private var channelNum: Int by Preference(Constant.CHANNEL_NUM, -1)
    override fun attachLayoutRes(): Int = R.layout.activity_main
    override fun initData() {

    }
    @SuppressLint("WrongConstant")
    override fun initView() {
        showFragment(index)
        //切换手持为0信道
        if (channelNum != 0){
          val mLoraManager = getSystemService(Context.LORA_SERVICE) as LoRaManager
            mLoraManager.changeWatchType(0)
            channelNum = 0
        }
    }

    private fun showFragment(index:Int) {
        val transaction = supportFragmentManager.beginTransaction()
        when(index){
            1->{
                if (practiceFragment == null) {
                    practiceFragment = PracticeFragment.getInstance()
                    transaction.add(R.id.container, practiceFragment, "practice")
                } else {
                    transaction.hide(currentFragment).show(practiceFragment)
                }
                currentFragment = practiceFragment
            }
            2->{
                if (taskFragment == null) {
                    taskFragment = TaskFragment.getInstance()
                    transaction.add(R.id.container, taskFragment, "task")
                } else {
                    transaction.hide(currentFragment).show(taskFragment)
                }
                currentFragment = taskFragment
            }

            3->{
                if (settingFragment == null) {
                    settingFragment = SettingFragment.getInstance()
                    transaction.add(R.id.container, settingFragment, "setting")
                } else {
                    transaction.hide(currentFragment).show(settingFragment)
                }
                currentFragment = settingFragment
            }
        }

        transaction.commit()
    }

    override fun initListener() {
        ll_practice.setOnClickListener {
            if (index==1) {
                return@setOnClickListener
            }
            iv_practice.setImageResource(R.mipmap.ic_practice_selected)
            tv_practice.setTextColor(resources.getColor(R.color.color_blue_2979FF))
            iv_task.setImageResource(R.mipmap.ic_task_normal)
            tv_task.setTextColor(resources.getColor(R.color.color_gray_424242))
            iv_setting.setImageResource(R.mipmap.ic_setting_normal)
            tv_setting.setTextColor(resources.getColor(R.color.color_gray_424242))
            index = 1
            showFragment(1)
        }


        ll_task.setOnClickListener {
            if (index==2) {
                return@setOnClickListener
            }
            iv_practice.setImageResource(R.mipmap.ic_practice_normal)
            tv_practice.setTextColor(resources.getColor(R.color.color_gray_424242))
            iv_task.setImageResource(R.mipmap.ic_task_selected)
            tv_task.setTextColor(resources.getColor(R.color.color_blue_2979FF))
            iv_setting.setImageResource(R.mipmap.ic_setting_normal)
            tv_setting.setTextColor(resources.getColor(R.color.color_gray_424242))
            index = 2
            showFragment(2)
        }

        ll_setting.setOnClickListener {
            if (index==3) {
                return@setOnClickListener
            }
            iv_practice.setImageResource(R.mipmap.ic_practice_normal)
            tv_practice.setTextColor(resources.getColor(R.color.color_gray_424242))
            iv_task.setImageResource(R.mipmap.ic_task_normal)
            tv_task.setTextColor(resources.getColor(R.color.color_gray_424242))
            iv_setting.setImageResource(R.mipmap.ic_setting_selected)
            tv_setting.setTextColor(resources.getColor(R.color.color_blue_2979FF))
            index = 3
            showFragment(3)
        }
    }
}